//商品列表
// let name = "模型手办";
let toke = localStorage.getItem("token");
if (toke) {
  let unlogin = document.querySelector("#unlogin");
  unlogin.style.display = "none";
  let logined = document.querySelector("#logined");
  logined.style.display = "block";
}
let productArr = [];
//商品列表
let page = 1;
let per = 12;
let category;
async function loadList() {
  // 获取当前选中的分类id
  let paramsObj = { page, per };
  if (category) {
    paramsObj.category = category;
  }
  let res = await myAxios.get(pListAPI, { params: paramsObj });
  console.log(res);
  productArr = res.data.data;
  console.log(productArr);
  renderList();
}
//渲染列表 商品订单
function renderList() {
  let liArr = productArr.map(
    (v) => `
    <li>
              <a href="../html/detail.html?id=${v.id}"  class="good-link" target="_blank">
                  <img src="http://192.168.18.95:1337${v.coverImage}" width="280" height="268" alt="活动图">
                  <div class="good-info">
                      <p class="good-name">${v.name}</p>
                      <p class="good-pri">￥${v.price}</p>
                  </div>
              </a>
          </li>`
  );
  document.querySelector(".fixlist").innerHTML = liArr.join("");
}
loadList();
//翻页功能
let index = 0;
let pagebtn = document.querySelectorAll(".page_btn");
pagebtn.forEach((v, i) => {
  v.onclick = function () {
    pagebtn[index].classList.remove("active");
    this.classList.add("active");
    let a = this.querySelector("a");
    page = a.innerText;
    index = i;
    loadList();
  };
});
// let loadMore = document.querySelector(".loadMore");
// loadMore.onclick = () => {
//   page = 2;
//   loadList();
// };
// cateList();

// //分类列表
// function cateList() {
//   myAxios.get(cateAPI, { params: { uid } }).then((r) => {
//     let menuLiStr = menuLi(r.data.data);

//     document.querySelector(".menu").innerHTML = menuLiStr;
//   });
// }
//商品分类id
async function cateList() {
  let cateid = await myAxios.get(cateAPI);
  console.log(cateid);
  let menuLiStr = menuLi(cateid.data.data);
  document.querySelector("#dj_class_ul").innerHTML += menuLiStr;
}
cateList();
function menuLi(menuArr) {
  let liArr = menuArr.map((v) => {
    return `
      <dd>
          <span onclick="menuClickHandle(${v.id},this)">${v.name}</span>
      </dd>
      `;
  });
  return liArr.join("");
}
function menuClickHandle(cate_id_temp, span) {
  console.log(cate_id_temp);

  let dl = span.parentNode.parentNode;
  console.log(dl);
  let dds = dl.querySelectorAll("dd");
  console.log(dds);
  for (let i = 0; i < dds.length; i++) {
    dds[i].classList.remove("cur");
  }
  let dd = span.parentNode;
  console.log(dd);
  dd.classList.add("cur");
  category = cate_id_temp;
  loadList();
}
//购物车商品数
let num = document.querySelector(".navico-cartnum");
console.log(num);
let cartarr = [];
async function loadCartList() {
  // 解构 出data属性
  let { data } = await myAxios.get(cartListAPI);
  cartarr = [...data.data];
  console.log(cartArr);
  num.innerHTML = cartarr.length;
}
loadCartList();
